package home.study.xxy.bs.controller;

import home.study.xxy.bs.entity.CountDay;
import home.study.xxy.bs.entity.ManagementText;
import home.study.xxy.bs.entity.Page;
import home.study.xxy.bs.entity.ResultList;
import home.study.xxy.bs.service.OtherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @title: OtherController
 * @功能:
 * @Author Xiao Xiangyu
 * @Date: 2021/5/1 16:02
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/otherController")
public class OtherController {


    @Autowired
    private OtherService otherService;

    @GetMapping("/getCountSum")
    public List<CountDay> countSum(){
        List<CountDay> contS =  otherService.countSum();
        if (contS!=null){
            log.info("流量查询成功");
            return contS;
        }else {
            log.error("流量查询错误");
            return null;
        }
    }

    @GetMapping("/addPQ")
    public boolean addPQ(ManagementText managementText){
        if (managementText.getTextType()!=null && managementText.getAnswer()!=null&&managementText.getShow()!=null){
            managementText.setCreateTime(new Date());
            int contS =  otherService.addPQ(managementText);
            if (contS == 1){
                log.info("公告添加成功");
                return true;
            }else {
                log.error("公告添加错误");
                return false;
            }
        }else{
            log.error("公告问题信息为空");
            return false;
        }
    }
    @GetMapping("/delete")
    public boolean deletePQ(String id){
       if (otherService.deletePQ(id)){
           log.info("删除成功");
           return true;
       }else{
           log.error("删除失败");
           return false;
       }
    }

    @GetMapping("/updatePQ")
    public boolean updatePQ(ManagementText managementText){
        managementText.setCreateTime(null);
       if (otherService.updatePQ(managementText)){
           log.info("修改成功");
           return true;
       }else{
           log.error("修改失败");
           return false;
       }
    }


    @GetMapping("/queryPQ")
    public ResultList<ManagementText> queryPQ(ManagementText managementText,Page pageVo){
        ResultList<ManagementText> resultList = otherService.queryPQ(managementText, pageVo);
        if (resultList!=null){
            log.info("查询公告成功");
            return resultList;
        }
        log.info("查询公告失败");
        return null;
    }

}
